package com.imooc.controller;

import com.imooc.service.ItemsESService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: foodie-dev
 * @description: hello控制器
 * @author: rZ
 * @create: 2020-02-21 17:06
 **/
@RestController
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsESService itemsESService;


    @GetMapping("hello")
    public Object Hello(){
        return "hello elasticsearch";
    }

    /**
     * 搜索商品列表
     * @param keywords
     * @param sort
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/es/search")
    public IMOOCJSONResult search(String keywords, String sort, Integer page, Integer pageSize){
        if (StringUtils.isBlank(keywords)) {
            IMOOCJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }

        page --;
        PagedGridResult result = itemsESService.searchItems(keywords,sort,page,pageSize);
        return IMOOCJSONResult.ok(result);
    }


}
